document.addEventListener('DOMContentLoaded', () => {
    const loginForm = document.getElementById('loginForm');
    const registerForm = document.getElementById('registerForm');
    const formTitle = document.getElementById('formTitle');

    // 登录表单提交
    loginForm.addEventListener('submit', async (e) => {
        e.preventDefault();

        const username = document.getElementById('loginUsername').value.trim();
        const password = document.getElementById('loginPassword').value.trim();

        if (!username || !password) {
            alert("账号或密码不能为空");
            return;
        }

        try {
            const response = await request.post('/user/login', {
                username: username,
                password: password
            });

            if (response.code === 200) {

                // 保存token
                localStorage.setItem('accessToken', response.data.accessToken);
                // 保存用户信息
                localStorage.setItem('userInfo', JSON.stringify(response.data.user));

                // 跳转到首页
                window.location.href = 'blog.html';
            }
        } catch (error) {
            console.error('登录失败:', error);
            alert("账号或密码错误");
        }
    });

    // 注册表单提交
    registerForm.addEventListener('submit', async (e) => {
        e.preventDefault();

        const username = document.getElementById('regUsername').value.trim();
        const password = document.getElementById('regPassword').value.trim();
        const email = document.getElementById('regEmail').value.trim();
        const nickname = document.getElementById('regNickname').value.trim();

        if (!username || !password || !email || !nickname) {
            alert("所有字段都必须填写");
            return;
        }

        try {
            const response = await request.post('/user/register', {
                username: username,
                password: password,
                email: email,
                nickname: nickname
            });

            if (response.code === 200) {
                alert("注册成功，请登录");
                toggleForm(); // 切换到登录表单
            }
        } catch (error) {
            console.error('注册失败:', error);
            alert("注册失败，请重试");
        }
    });

    // 切换表单显示
    window.toggleForm = function () {
        const isLogin = loginForm.style.display === 'block';
        loginForm.style.display = isLogin ? 'none' : 'block';
        registerForm.style.display = isLogin ? 'block' : 'none';
        formTitle.textContent = isLogin ? 'REGISTER' : 'LOGIN';
    }

    // 背景动画效果
    const back = document.getElementById('back');
    window.onmousemove = function (event) {
        const x = -event.clientX / 10;
        const y = -event.clientY / 15;
        back.style.backgroundPositionX = x + "px";
        back.style.backgroundPositionY = y + "px";
    }

    // 加载动画
    const con = document.getElementById('con');
    function loadoff() {
        con.style.display = "none";
    }
    function loadon() {
        con.style.display = "flex";
    }
    loadon();
    setTimeout(loadoff, 3000);
});

